<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Apache,LAMP," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="一．官方说法&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;301，302 都是HTTP状态的编码，都代表着某个URL发生了转移，不同之处在于：  &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;301 redirect: 301 代表永久性转移(Permanently Moved)。 &amp;#160;&amp;#">
<meta name="keywords" content="Apache,LAMP">
<meta property="og:type" content="article">
<meta property="og:title" content="HTTP返回码中301与302的区别">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Apache/26.  HTTP返回码中301与302的区别/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="一．官方说法&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;301，302 都是HTTP状态的编码，都代表着某个URL发生了转移，不同之处在于：  &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;301 redirect: 301 代表永久性转移(Permanently Moved)。 &amp;#160;&amp;#">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-01T08:42:15.247Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HTTP返回码中301与302的区别">
<meta name="twitter:description" content="一．官方说法&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;301，302 都是HTTP状态的编码，都代表着某个URL发生了转移，不同之处在于：  &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;301 redirect: 301 代表永久性转移(Permanently Moved)。 &amp;#160;&amp;#">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Apache/26.  HTTP返回码中301与302的区别/"/>





  <title>HTTP返回码中301与302的区别 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Apache/26.  HTTP返回码中301与302的区别/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">HTTP返回码中301与302的区别</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:00:30+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="一．官方说法"><a href="#一．官方说法" class="headerlink" title="一．官方说法"></a>一．官方说法</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;301，302 都是HTTP状态的编码，都代表着某个URL发生了转移，不同之处在于： </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;301 redirect: 301 代表永久性转移(Permanently Moved)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;302 redirect: 302 代表暂时性转移(Temporarily Moved )。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是很官方的说法，那么它们的区别到底是什么呢？</p>
<h2 id="二．现实中的差异"><a href="#二．现实中的差异" class="headerlink" title="二．现实中的差异"></a>二．现实中的差异</h2><h3 id="2-1-对于用户"><a href="#2-1-对于用户" class="headerlink" title="2.1.对于用户"></a>2.1.对于用户</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;301，302对用户来说没有区别，他们看到效果只是一个跳转，浏览器中旧的URL变成了新的URL。页面跳到了这个新的url指向的地方。</p>
<h3 id="2-2-对于引擎及站长"><a href="#2-2-对于引擎及站长" class="headerlink" title="2.2.对于引擎及站长"></a>2.2.对于引擎及站长</h3><h4 id="2-2-1-302"><a href="#2-2-1-302" class="headerlink" title="2.2.1 302"></a>2.2.1 302</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;302转向可能会有URL规范化及网址劫持的问题。可能被搜索引擎判为可疑转向，甚至认为是作弊。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;网址规范化</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考：<a href="https://hcldirgit.github.io/2017/07/27/27.%20URL%E7%BD%91%E5%9D%80%E8%A7%84%E8%8C%83%E5%8C%96/">URL网址规范化</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;网址劫持</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;302重定向和网址劫持（URL hijacking）有什么关系呢？这要从搜索引擎如何处理302转向说起。从定义来说，从网址A做一个302重定向到网址B时，主机服务器的隐含意思是网址A随时有可能改主意，重新显示本身的内容或转向其他的地方。大部分的搜索引擎在大部分情况下，当收到302重定向时，一般只要去抓取目标网址就可以了，也就是说网址B。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实际上如果搜索引擎在遇到302转向时，百分之百的都抓取目标网址B的话，就不用担心网址URL劫持了。问题就在于，有的时候搜索引擎，尤其是Google，并不能总是抓取目标网址。为什么呢？比如说，有的时候A网址很短，但是它做了一个302重定向到B网址，而B网址是一个很长的乱七八糟的URL网址，甚至还有可能包含一些问号之类的参数。很自然的，A网址更加用户友好，而B网址既难看，又不用户友好。这时Google很有可能会仍然显示网址A。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于搜索引擎排名算法只是程序而不是人，在遇到302重定向的时候，并不能像人一样的去准确判定哪一个网址更适当，这就造成了网址URL劫持的可能性。也就是说，一个不道德的人在他自己的网址A做一个302重定向到你的网址B，出于某种原因， Google搜索结果所显示的仍然是网址A，但是所用的网页内容却是你的网址B上的内容，这种情况就叫做网址URL劫持。你辛辛苦苦所写的内容就这样被别人偷走了。</p>
<h4 id="2-2-2-301"><a href="#2-2-2-301" class="headerlink" title="2.2.2 301"></a>2.2.2 301</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当网页A用301重定向转到网页B时，搜索引擎可以肯定网页A永久的改变位置，或者说实际上不存在了，搜索引擎就会把网页B当作唯一有效目标。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;301的好处是:</p>
<ul>
<li>没有网址规范化问题。</li>
<li>也很重要的，网页A的PR网页级别会传到网页B。</li>
</ul>
<h2 id="三．Apache中实现301、302"><a href="#三．Apache中实现301、302" class="headerlink" title="三．Apache中实现301、302"></a>三．Apache中实现301、302</h2><h3 id="方法一，url-rewrite，mod-rewrite"><a href="#方法一，url-rewrite，mod-rewrite" class="headerlink" title="方法一，url rewrite，mod_rewrite"></a>方法一，url rewrite，mod_rewrite</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">Rewriteengine on </div><div class="line"> RewriteCond %&#123;HTTP_HOST&#125; ^cmp.soso.com [NC] </div><div class="line"> RewriteRule ^/js/(.*) http://www.soso.com/js/<span class="variable">$1</span> [R=301] </div><div class="line"> ServerName cmp.soso.com</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将cmp.soso.com中js目录的下所有访问重定向到<a href="http://www.soso.com/js/，指定跳转返回码为301。" target="_blank" rel="external">http://www.soso.com/js/，指定跳转返回码为301。</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于[R=301]的详解：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;’redirect|R [=code]’ (强制重定向 redirect)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以<a href="http://thishost[:thisport]/(使新的URL成为一个URI" target="_blank" rel="external">http://thishost[:thisport]/(使新的URL成为一个URI</a>) 为前缀的Substitution可以强制性执行一个外部重定向。 如果code没有指定，则产生一个HTTP响应代码302(临时性移动)。 如果需要使用在300-400范围内的其他响应代码，只需在此指定这个数值即可， 另外，还可以使用下列符号名称之一: temp (默认的), permanent, seeother. 用它可以把规范化的URL反馈给客户端，如, 重写``/~\’’为 ``/u/‘’，或对/u/user加上斜杠，等等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意: 在使用这个标记时，必须确保该替换字段是一个有效的URL! 否则，它会指向一个无效的位置! 并且要记住，此标记本身只是对URL加上 <a href="http://thishost[:thisport]/的前缀，重写操作仍然会继续。" target="_blank" rel="external">http://thishost[:thisport]/的前缀，重写操作仍然会继续。</a> 通常，你会希望停止重写操作而立即重定向，则还需要使用’L’标记.</p>
<h3 id="方法二-Redirect-，涉及模块：mod-alias"><a href="#方法二-Redirect-，涉及模块：mod-alias" class="headerlink" title="方法二 Redirect ，涉及模块：mod_alias"></a>方法二 Redirect ，涉及模块：mod_alias</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;VirtualHost 10.1.146.163:80&gt; </div><div class="line">  DocumentRoot /home/qmhball/web/mybranches/stat_3276/oa/ </div><div class="line">  ServerName oalogin.com </div><div class="line">  Redirect 301 /login.php http://www.soso.com </div><div class="line">&lt;/VirtualHost&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将oalogin.com下对login.PHP的访问重定向到<a href="http://www.soso.com，返回码301。" target="_blank" rel="external">http://www.soso.com，返回码301。</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果没有指定redirect的返回参数（例中的301），则默认重定向是”临时性的”(HTTP status 302)。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Apache/" rel="tag"># Apache</a>
          
            <a href="/tags/LAMP/" rel="tag"># LAMP</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Apache/25. Apache域名301跳转/" rel="next" title="Apache域名301跳转">
                <i class="fa fa-chevron-left"></i> Apache域名301跳转
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Apache/27. URL网址规范化/" rel="prev" title="URL网址规范化">
                URL网址规范化 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#一．官方说法"><span class="nav-number">1.</span> <span class="nav-text">一．官方说法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二．现实中的差异"><span class="nav-number">2.</span> <span class="nav-text">二．现实中的差异</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-对于用户"><span class="nav-number">2.1.</span> <span class="nav-text">2.1.对于用户</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-对于引擎及站长"><span class="nav-number">2.2.</span> <span class="nav-text">2.2.对于引擎及站长</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-302"><span class="nav-number">2.2.1.</span> <span class="nav-text">2.2.1 302</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-301"><span class="nav-number">2.2.2.</span> <span class="nav-text">2.2.2 301</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#三．Apache中实现301、302"><span class="nav-number">3.</span> <span class="nav-text">三．Apache中实现301、302</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#方法一，url-rewrite，mod-rewrite"><span class="nav-number">3.1.</span> <span class="nav-text">方法一，url rewrite，mod_rewrite</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#方法二-Redirect-，涉及模块：mod-alias"><span class="nav-number">3.2.</span> <span class="nav-text">方法二 Redirect ，涉及模块：mod_alias</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
